<template>
    <div class="upload-file">
        <el-form :model="registerMsg" ref="ruleForm" label-width="100px">

            <el-form-item
                    prop="companyName"
                    status-icon
                    label="公司名称"
                    :rules="[
       {required: true,message:'请输入企业名称',trigger:['blur','change']}

              ]"
            >
                <el-input size="small" v-model="registerMsg.companyName" placeholder="请输入内容"></el-input>
            </el-form-item>

            <el-form-item
                    status-icon
                    prop="email"
                    label="邮箱"
                    :rules="[
      { required: true, message: '请输入邮箱地址', trigger: 'blur' },
      { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
    ]"
            >
                <el-input size="small" placeholder="请输入邮箱" v-model="registerMsg.email"></el-input>
            </el-form-item>

            <el-form-item
                    prop="legalPerson"
                    status-icon
                    label="法人代表"
                    :rules="[
       {required: true,message:'请输入法人代表',trigger:['blur','change']}

              ]"
            >
                <el-input size="small" v-model="registerMsg.legalPerson" placeholder="请输入内容"></el-input>
            </el-form-item>

            <el-form-item
                    prop="address"
                    status-icon
                    label="地址"
                    :rules="[
       {required: true,message:'请输入地址',trigger:['blur','change']}

              ]"
            >
                <el-input size="small" v-model="registerMsg.address" placeholder="请输入内容"></el-input>
            </el-form-item>

            <el-form-item
                    status-icon
                    prop="legalRegTime"
                    label="注册日期"
                    :rules="[
      { required: true, message: '请选择日期', trigger: 'blur' }
    ]"
            >
                <el-date-picker size="small" type="date" placeholder="选择日期" v-model="registerMsg.legalRegTime" > </el-date-picker>
            </el-form-item>
        </el-form>
        <el-upload
                accept=".png,.jpg,.jpeg"
                ref="upload"
                multiple
                :limit="5"
                name="multipartFiles"
                action="http://liuzy.hicp.net/file/upload"
                :on-change="handleChange"
                :on-remove="handleRemove"
                :on-exceed="handleExceed"
                :file-list="fileList"
                :http-request="uploadFile"
                :auto-upload="false">
            <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
            <el-button style="margin-left: 133px;" size="small" type="success" @click="submitUpload">上传到服务器
            </el-button>
            <div slot="tip" class="el-upload__tip">只能上传xlsx文件，且不超过100m</div>
        </el-upload>
    </div>
</template>

<script>
    import axios from 'axios'
    export default {
        name: "upload",
        data() {
            return {
                fileData: '',  // 文件上传数据（多文件合一）
                fileList: [],   // upload多文件数组
                registerMsg: {
                    companyName: '',      //企业名称
                    legalPerson: '',      //法人
                    legalRegTime: '',     //注册时间
                    email: '',      //企业邮箱,
                    address: ''       //企业地址
                },
                uploadData: {
                    fieldData: {

                    }
                },

            }
        },
        methods:{
            // 上传文件
            uploadFile(file) {
                this.fileData.append('bytes', file.file);  // append增加数据
            },

            // 上传到服务器
            submitUpload() {
                let fieldData = this.uploadData.fieldData;  // 缓存，注意，fieldData不要与fileData看混

              if (this.fileList.length === 0) {
                    this.$message({
                        message: '请先选择文件',
                        type: 'warning'
                    })
                } else {
                    const isLt100M = this.fileList.every(file => file.size / 1024 / 1024 < 100);
                    if (!isLt100M) {
                        this.$message.error('请检查，上传文件大小不能超过100MB!');
                    } else {
                        this.fileData = new FormData();  // new formData对象
                        this.$refs.upload.submit();  // 提交调用uploadFile函数
                        // this.fileData.append('companyName', this.registerMsg.companyName);  // 添加机构id
                        // this.fileData.append('legalPerson', this.registerMsg.legalPerson);  // 添加token
                        // this.fileData.append('legalRegTime',this.registerMsg.legalRegTime)
                        // this.fileData.append('email',this.registerMsg.email)
                        // this.fileData.append('address',this.registerMsg.address)
                        axios.post("http://liuzy.hicp.net/file/upload",this.fileData).then((response) => {
                            console.log(response)
                            if (response.data.code === 100) {
                                this.$message({
                                    message: "上传成功",
                                    type: 'success'
                                });
                                this.fileList = [];
                            } else {
                                this.$message({
                                    message: response.data.desc,
                                    type: 'error'
                                })
                            }
                        });
                    }
                }
            },

            //移除
            handleRemove(file, fileList) {
                this.fileList = fileList;
                // return this.$confirm(`确定移除 ${ file.name }？`);
            },

            // 选取文件超过数量提示
            handleExceed(files, fileList) {
                this.$message.warning(`当前限制选择 5 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
            },

            //监控上传文件列表
            handleChange(file, fileList) {
                let existFile = fileList.slice(0, fileList.length - 1).find(f => f.name === file.name);
                if (existFile) {
                    this.$message.error('当前文件已经存在!');
                    fileList.pop();
                }
                this.fileList = fileList;
            },
        }

    }
</script>

<style scoped>

</style>